var topObj = this;
var request = "docs";
var xhr = new XMLHttpRequest();
xhr.open("GET",request, true);
xhr.onreadystatechange = loadVis;

google.setOnLoadCallback(requestData);
google.load('visualization', '1', {packages: ['corechart']});



function loadVis() {
	if(this.readyState == 4){
		//alert("readyState is 4");
		json = JSON.parse(this.responseText);
		drawVisualization.call(topObj,json);
	}
}


function requestData() {

	//alert("xhr");
	xhr.send(null);
}



function drawVisualization(json) {
	//alert("in draw:");
	var data = new google.visualization.DataTable();
	data.addColumn('string', 'buildId');//col 0

	var fqes = new Object();
	for( var i in json )
		fqes[json[i].fqe] = 0;

	col = 1;
	for(var fqe in fqes){
		data.addColumn('number', fqe);
		fqes[fqe] = col++;
	}

	var buildMax = 2;//max buildId + 1
	data.addRows(buildMax);
	for(i=0;i<buildMax;i++){
		data.setValue(i, 0, String(i));
		for(var j=0;j<json.length;j++) {
			data.setValue(json[j].buildId, fqes[json[j].fqe], parseFloat(json[j].rs_time_avg));
		}
	}

	/*data.addColumn('string','buildId');
	data.addColumn('number','1');
	data.addColumn('number','2');
	data.addColumn('number','3');
	data.addColumn('number','4');
	data.addRows(2);
	data.setValue(0,0,'0')
	data.setValue(0,1,313);
	data.setValue(0,2,480);
	data.setValue(0,3,772.5);
	data.setValue(0,4,249.5);
	data.setValue(1,0,'1')
	data.setValue(1,1,657.5);
	data.setValue(1,2,832.5);
	data.setValue(1,3,265.5);
	data.setValue(1,4,643.5);*/
	
	
	var chart = new google.visualization.LineChart(document.getElementById('visualization'));
	chart.draw(data, {width: 1200, height: 400, title: 'Metadata Baseline',legend:'bottom'});
}
